import { _decorator, Component, Node, SpriteFrame, AnimationClip } from "cc";
import { Animation01 } from "./../Animations/Animation01";
import { Communicate } from "./../Comunicate/Communicate";
import { EventType, CustomEventData } from "./../Enums/EnumPool";
const { ccclass, property } = _decorator;

@ccclass("Test01")
export class Test01 extends Component {
  imgFolderPath: string = "sports/002";
  playerNode: Node;
  spriteFrameArray: SpriteFrame[] = [];
  animationComp01: Animation01;

  handleEvent(data?: any) {
    console.log(this.constructor.name, "handleEvent:", data);
    console.log("Before", this.animationComp01.isPlayingAnimation);

    if (data[0] === CustomEventData.WALKING_STOP) {
      this.animationComp01.isPlayingAnimation = false;
      console.log("After", this.animationComp01.isPlayingAnimation);
    } else if (data[0] === CustomEventData.WALKING_START) {
      this.animationComp01.isPlayingAnimation = true;
      console.log("After", this.animationComp01.isPlayingAnimation);
    }

    this.animationComp01.checkOutAnimationPlay();
  }

  animationMethod() {
    this.animationComp01.localNode = this.playerNode;
    this.animationComp01.clipName = "TheDefaultAnimationClip";
    this.animationComp01.wrapMode = AnimationClip.WrapMode.Loop;
    this.animationComp01.imgFolderPath = this.imgFolderPath;
    this.animationComp01.clipDuration = 1;
    this.animationComp01.frameRate = 10;

    this.animationComp01.animationInitialize();
  }

  initialize() {
    const stageNode = new Node("StageNode");
    this.playerNode = new Node("PlayerNode");

    stageNode.addChild(this.playerNode);
    this.node.addChild(stageNode);

    this.playerNode.addComponent(Animation01);
    this.animationComp01 = this.playerNode.getComponent(Animation01);
  }

  start() {
    console.info(this.node);
    this.animationMethod();
  }

  protected onLoad(): void {
    this.initialize();

    const comm: Communicate = Communicate.getInstance;
    comm.on(EventType.IDENTIFY_FIRST, this.handleEvent, this);
  }
}
